$(function() {
	var teaRecent = $("#teaRecent");
	teaRecent.datagrid({
		url : 'teaemploy/teaemployment',
		fit : true,
		rownumbers : true,
		pagination : true,
		singleSelect:true,
		fitColumns : true,
		emptyMsg:"<img src='public/images/NOJYXX.png'>",
		columns : [ [
				{
					field : 'student.name',
					title : '姓名',
					align : 'center',
					width : 100,
					formatter : function(value, rec) {
						return rec.student['name'];
					}
				},
				{
					field : 'student.gender',
					title : '性别',
					align : 'center',
					width : 80,
					formatter : function(value, rec) {
						if(rec.student['gender']=='男'){
							return '<img src="public/images/male.png" title="我是帅锅">'
						}else{
							return '<img src="public/images/female.png" title="我是妹子">'
						}
					}
				},
				{
					field : 'company',
					title : '公司名称',
					align : 'center',
					
					width : 150
				},
				{
					field : 'offer.path',
					title : 'offer',
					align : 'center',
					hidden : 'true',
					width : 80,
					formatter : function imgFormatter(value, rec) {
						if(rec.offer == null){
							return '';
						}
						return rec.offer['path'];
					}
				},{
					field : 'address',
					title : '公司地址',
					align : 'center',
					width : 150
				},
				{
					field : 'trialSalary',
					title : '试用薪水',
					align : 'center',
					width : 80
				},
				{
					field : 'salary',
					title : '薪水',
					align : 'center',
					width : 80
				},
				{
					field : 'gdtime',
					title : '毕业时间',
					align : 'center',
					width : 100
				},
				{
					field : 'getjobtime',
					title : '入职时间',
					align : 'center',
					width : 100
				},
				{
					field : 'ac',
					title : '操作',
					width : 200,
					align : 'center',
					formatter : function(value, row, index) {
						var bts = [];
						bts.push('<a data-index="' + index
								+ '" class="ctr edit">编辑</a>');
						bts.push('<a data-id="' + row.id + '" data-index="'
								+ index + '" class="ctr guidang">归档</a>');
						if(teaRecent.datagrid('getData').rows[index].offer != null){
							bts.push('<a id="' + row.id + '" data-id="' + row.id
									+ '" data-index="' + index
									+ '" class="ctr" target="_Blank" href="teaemploy/saveGraduation/'+teaRecent.datagrid('getData').rows[index].offer.path+'">offer预览</a>');
						}
						return bts.join('');
					}
				} ] ]
	});

	var name = $("#stuname");
	$("#stuSearch").on('click', function() {
		teaRecent.datagrid("load", {
			name : "%" + name.val() + "%"
		});
	});

	var form = $("#stuSearchForm");
	$("#stuReset").on('click', function() {
		form.form('clear');
		teaRecent.datagrid("load", {});
	});
	var guidang = $("#whStudentGrid");
	var stuPanel = teaRecent.datagrid("getPanel");
	stuPanel.on("click", "a.delete", function() {
		
		var index = this.dataset.index;
		var id = this.dataset.id;
		$.messager.confirm("提示", "您确定要打回此学员吗？", function(r) {
			if (r) {
				$.get("teaemploy/delete/" + id, function(rs) {
					if (rs.success) {
						teaRecent.datagrid("reload");
					} else {
						var obj = JSON.parse(rs);
						$.messager.alert("提示", obj.message);
					}
				});
			}
		});
	}).on("click", "a.edit", function() {
			var index = this.dataset.index;
			// 如果只有下标，没有id的情况，要获取id
			var rows = teaRecent.datagrid("getRows");
			// 同下标获取对应行的数据对象
			var id = rows[index].id;
			userForm(id);
		}).on("click", "a.guidang", function() {
			var index = this.dataset.index;
			var id = this.dataset.id;
			$.messager.confirm("提示", "您确定要归档吗？", function(r) {
				if (r) {
					$.get("teaemploy/guidang/" + id, function(rs) {
						if (rs.success) {
							// 刷新表格
							guidang.datagrid("reload");
							teaRecent.datagrid("reload");
						} else {
							var obj = JSON.parse(rs);
							$.messager.alert("提示", obj.message);
						}
					});
				}
		});
	});
	function userForm(id) {
		// 创建窗口
		var dialog = $("<div/>").dialog({
			href : 'teaemploy/addteaemployment' + (id ? ("/" + id) : ""),
			title : "就业学员信息",
			height : 430,
			width : 680,
			modal : true,
			onClose : function() {
				// 窗口关闭的同时销毁此窗口
				$(this).dialog("destroy");
			},
			buttons : [ {
				iconCls : 'icon-save',
				text : '提交',
				handler : function() {
					$("#userForm").form('submit', {
						url : 'teaemploy/saveGraduation',
						data : $("#userForm").serialize(),
						success : function(rs) {
							var obj=JSON.parse(rs);
							if (obj.success) {
								// 刷新数据表格
								teaRecent.datagrid("reload");
								// 关闭窗口
								dialog.dialog("close");
							} else {
								$.messager.alert("提示", obj.message);
							}
						}
					});
				}
			} ]
		});
	}
 });